package io.renren.modules.test.library.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.R;
import io.renren.modules.module.entity.Module;
import io.renren.modules.module.service.ModuleServicex;
import io.renren.modules.product.entity.ProductEntity;
import io.renren.modules.product.service.ProductService;
import io.renren.modules.test.library.entity.Library;
import io.renren.modules.test.library.service.LibraryService;
import io.renren.modules.test.usecase.entity.UseCase;
import io.renren.modules.test.usecase.entity.UseCaseLibrary;
import io.renren.modules.test.usecase.service.CaseLibraryService;
import io.renren.modules.test.usecase.service.CaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liyinuo
 * 测试库
 */
@RestController
@RequestMapping("/library")
@Api(tags = "测试库管理")
public class LibraryController {
    @Autowired
    private LibraryService libraryService;

    @Autowired
    private CaseLibraryService caseLibraryService;

    @Autowired
    private CaseService caseService;

    @Autowired
    private ModuleServicex moduleServicex;

    @Autowired
    private ProductService productService;

    @PostMapping("/insert")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @ApiOperation("新建测试库")
    @RequiresPermissions("library:save")
    public R insertLibrary(@RequestBody Library library){
        libraryService.save(library);
        return R.ok("添加成功");
    }

    @GetMapping("/delete/{id}")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @ApiOperation("删除测试库")
    @Transactional
    @RequiresPermissions("library:delete")
    public R deleteLibrary(@PathVariable("id") Long id){
        libraryService.removeLibrary(id);
        return R.ok("删除成功");
    }

    @PostMapping("/update")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @ApiOperation("修改测试库")
    @RequiresPermissions("library:update")
    public R updateLibrary(@RequestBody Library library){
        libraryService.updateById(library);
        return R.ok("修改成功");
    }

    @GetMapping("/list")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @ApiOperation("分页查询")
    @RequiresPermissions("library:list")
    public R listLibrary(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                         @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                         @RequestParam(name="libraryName", defaultValue="") String libraryName){

        IPage<Library> pageList = libraryService.getListLibrary(pageNo,pageSize,libraryName);
        return R.ok("200").put("data",pageList);
    }

    @GetMapping("/selectOne/{id}")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @ApiOperation("通过id查询单个")
    @RequiresPermissions("library:info")
    public R selectOne(@PathVariable("id") Long id){
        Library library = libraryService.getById(id);
        return R.ok("200").put("data",library);
    }
    @GetMapping("/moduleInfo/{id}")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @ApiOperation("通过测试库查询对应产品下的所有模块信息")
    @RequiresPermissions("library:moduleInfo")
    public R moduleInfo(@PathVariable("id") Long id){
        Long productId = productService.getById(libraryService.getById(id).getProductId()).getProductId();
        List<Module> list = moduleServicex.list(new QueryWrapper<Module>().eq("product_id", productId));
        return R.ok().put("data",list);
    }
}
